import {
    addAdConfigruation,
    delAdConfigruation,
    queryAdConfigruation,
    modAdConfigruation,
    queryAllAdConfigruation,
} from '../services/operation'

export default {
    namespace: 'adConfigruationEditAndAdd',
    state:{
        data:{
            list: [],
            pagination: {},
        },
        adCList: [],
        loading: false,
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryAdConfigruation, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },


        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(addAdConfigruation, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        * mod({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modAdConfigruation, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delAdConfigruation, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        * queryAllAdConfigruation({payload, callback}, {call, put}){
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });
            const response = yield call(queryAllAdConfigruation, payload);
            // yield put({
            //    type: 'save',
            //    payload: response,
            //});
            //yield put({
            //    type: 'changeLoading',
            //    payload: false,
            //});
            if (callback) callback(response);
        },
    },

    reducers: {
        save(state, action){
            const adCList = [];
            getAdCList(adCList, action.payload.resultData.adConfigruationList);
            function getAdCList(tmpR, list) {
                for (var i = 0; i < list.length; i++){
                    const adcItem = {value: list[i]["seat_id"], label: list[i]["seat_code"]};
                    if (list[i]["children"]){
                        adcItem["children"] = [];
                        getAdCList(adcItem["children"], list[i]["children"]);
                    }

                    tmpR.push(adcItem);
                }
            }
            console.log(action.payload.resultData.adConfigruationList);
            return{
                ...state,
                adCList: adCList,
                data: action.payload.resultData.adConfigruationList,
            };
        },

        changeLoading(state, action){
            return{
                ...state,
                loading: action.payload,
            };
        },
    },
};